/*
 * ImPduClient.cpp
 * Interactive Packet with client
 *
 *  Created on: 2013-8-27
 *      Author: ziteng@mogujie.com
 */

#include "ImPduClient.h"


CImPduLoginReq::CImPduLoginReq(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nUserId;
    is >> m_nClientType;
    
    PARSE_PACKET_ASSERT
}

CImPduLoginReq::CImPduLoginReq(uint32_t nUserId, uint16_t nClientType)
{
    m_pdu_header.command_id = IM_PDU_LOGIN_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nUserId;
    os << nClientType;
    
    WriteHeader();
}

CImPduLoginReq:: ~CImPduLoginReq()
{
    
}


CImPduLoginRes::CImPduLoginRes(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nStatus;
    
    PARSE_PACKET_ASSERT
}

CImPduLoginRes::CImPduLoginRes(uint16_t nStatus)
{
    m_pdu_header.command_id = IM_PDU_LOGIN_RES;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nStatus;
    
    WriteHeader();
}

CImPduLoginRes::~CImPduLoginRes()
{
    
}

CImPduInitCallReq::CImPduInitCallReq(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nToId;
    is >> m_nCallId;
    uint32_t nBodyLen = 0;
    char* pBody = is.ReadString(nBodyLen);
    m_strBody = string(pBody, nBodyLen);
    
    PARSE_PACKET_ASSERT
}

CImPduInitCallReq::CImPduInitCallReq(uint32_t nToId, uint32_t nCallId,const char* pBody, uint32_t nLen)
{
    m_pdu_header.command_id = IM_PDU_INIT_CALL_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nToId;
    os << nCallId;
    os.WriteString(pBody, nLen);
    
    WriteHeader();
}

CImPduInitCallReq::~CImPduInitCallReq()
{
    
}

CImPduInitCallRes::CImPduInitCallRes(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nToId;
    is >> m_nCallId;
    is >> m_nCode;
    uint32_t nStrLen = 0;
    char* pStr = is.ReadString(nStrLen);
    
    m_strReason = string(pStr, nStrLen);
    
    uint32_t nBodyLen = 0;
    char* pBody = is.ReadString(nBodyLen);
    
    m_strBody = string(pBody, nBodyLen);
    
    PARSE_PACKET_ASSERT
}

CImPduInitCallRes::CImPduInitCallRes(uint32_t nToId, uint32_t nCallId, uint16_t nCode, const string& strReason, const char* pBody, uint32_t nLen)
{
    m_pdu_header.command_id = IM_PDU_INIT_CALL_RES;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nToId;
    os << nCallId;
    os << nCode;
    os.WriteString(strReason.c_str(), strReason.size());
    os.WriteString(pBody, nLen);
    
    WriteHeader();
}

CImPduInitCallRes::~CImPduInitCallRes()
{
    
}

CImPduCallNotifyReq::CImPduCallNotifyReq(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nFromId;
    is >> m_nCallId;
    uint32_t nBodyLen = 0;
    char* pBody = is.ReadString(nBodyLen);
    m_strBody = string(pBody, nBodyLen);
    
    PARSE_PACKET_ASSERT
}

CImPduCallNotifyReq::CImPduCallNotifyReq(uint32_t nFromId, uint32_t nCallId, const char* pBody, uint32_t nLen)
{
    m_pdu_header.command_id = IM_PDU_CALL_NOTIFY_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nFromId;
    os << nCallId;
    
    os.WriteString(pBody, nLen);
    
    WriteHeader();
}

CImPduCallNotifyReq::~CImPduCallNotifyReq()
{
    
}

CImPduCallNotifyRes::CImPduCallNotifyRes(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nFromId;
    is >> m_nCallId;
    is >> m_nCode;
    uint32_t nStrLen = 0;
    char* pStr = is.ReadString(nStrLen);
    m_strReason = string(pStr, nStrLen);
    
    uint32_t nBodyLen = 0;
    char* pBody = is.ReadString(nBodyLen);
    m_strBody = string(pBody, nBodyLen);
    
    PARSE_PACKET_ASSERT
}

CImPduCallNotifyRes::CImPduCallNotifyRes(uint32_t nFromId, uint32_t nCallId, uint16_t nCode, const string& strReason, const char* pBody, uint32_t nLen)
{
    m_pdu_header.command_id = IM_PDU_CALL_NOTIFY_RES;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nFromId;
    os << nCallId;
    os << nCode;
    os.WriteString(strReason.c_str(), strReason.size());
    os.WriteString(pBody, nLen);
    
    WriteHeader();
}

CImPduCallNotifyRes::~CImPduCallNotifyRes()
{
    
}

CImPduHungUpReq::CImPduHungUpReq(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nPeerId;
    is >> m_nCallId;
    
    PARSE_PACKET_ASSERT
}

CImPduHungUpReq::CImPduHungUpReq(uint32_t nPeerId, uint32_t nCallId)
{
    m_pdu_header.command_id = IM_PDU_HUNG_UP_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nPeerId;
    os << nCallId;
    
    WriteHeader();
}

CImPduHungUpReq::~CImPduHungUpReq()
{
    
}


CImPduHungUpRes::CImPduHungUpRes(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nPeerId;
    is >> m_nCallId;
    
    PARSE_PACKET_ASSERT
}

CImPduHungUpRes::CImPduHungUpRes(uint32_t nPeerId, uint32_t nCallId)
{
    m_pdu_header.command_id = IM_PDU_HUNG_UP_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nPeerId;
    os << nCallId;
    
    WriteHeader();
}

CImPduHungUpRes::~CImPduHungUpRes()
{
    
}

CImPduHungUpNotifyReq::CImPduHungUpNotifyReq(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nPeerId;
    is >> m_nCallId;
    
    PARSE_PACKET_ASSERT
}

CImPduHungUpNotifyReq::CImPduHungUpNotifyReq(uint32_t nPeerId, uint32_t nCallId)
{
    m_pdu_header.command_id = IM_PDU_HUNG_UP_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nPeerId;
    os << nCallId;
    
    WriteHeader();
}

CImPduHungUpNotifyReq::~CImPduHungUpNotifyReq()
{
    
}

CImPduHungUpNotifyRes::CImPduHungUpNotifyRes(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nPeerId;
    is >> m_nCallId;
    
    PARSE_PACKET_ASSERT
}

CImPduHungUpNotifyRes::CImPduHungUpNotifyRes(uint32_t nPeerId, uint32_t nCallId)
{
    m_pdu_header.command_id = IM_PDU_HUNG_UP_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nPeerId;
    os << nCallId;
    
    WriteHeader();
}

CImPduHungUpNotifyRes::~CImPduHungUpNotifyRes()
{
    
}

CImPduOnlineUserReq::CImPduOnlineUserReq(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    
    if(nLen != IM_PDU_HEADER_LEN) {
        throw CPduException(m_pdu_header.command_id, ERROR_CODE_PARSE_FAILED, "parse packet failed");
    }
}

CImPduOnlineUserReq::CImPduOnlineUserReq()
{
    m_pdu_header.command_id = IM_PDU_ONLINE_USER_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    WriteHeader();
}

CImPduOnlineUserReq::~CImPduOnlineUserReq()
{
    
}


CImPduOnlineUserRes::CImPduOnlineUserRes(uchar_t* pBuf, uint32_t nLen)
{
    ReadPduHeader(pBuf, IM_PDU_HEADER_LEN, &m_pdu_header);
    CByteStream is(pBuf + IM_PDU_HEADER_LEN, nLen - IM_PDU_HEADER_LEN);
    
    is >> m_nUserCnt;
    
    for(uint32_t i=0; i<m_nUserCnt; ++i)
    {
        uint32_t nUserId = 0;
        is >> nUserId;
        m_lsUsers.push_back(nUserId);
    }
    
    PARSE_PACKET_ASSERT
}

CImPduOnlineUserRes::CImPduOnlineUserRes(uint32_t nUserCnt, const list<uint32_t>& lsUsers)
{
    m_pdu_header.command_id = IM_PDU_HUNG_UP_REQ;
    CByteStream os(&m_buf, IM_PDU_HEADER_LEN);
    m_buf.Write(NULL, IM_PDU_HEADER_LEN);
    
    os << nUserCnt;
    list<uint32_t>::const_iterator it = lsUsers.begin();
    for (; it!=lsUsers.end(); ++it) {
        os << *it;
    }
    
    WriteHeader();
}

CImPduOnlineUserRes::~CImPduOnlineUserRes()
{
    
}

//end: add by @wuhui 2015年5月14日

